package com.tencent.org.controller;


import com.tencent.basic.annotation.PreAuthorize;
import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.ExcelUtil;
import com.tencent.basic.util.PageList;
import com.tencent.org.domain.Shop;
import com.tencent.org.domain.ShopAuditLog;
import com.tencent.org.dto.ShopDto;
import com.tencent.org.query.ShopQuery;
import com.tencent.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(value = "店铺的API",description="店铺接口") // 打在Controller对Controller做描述 - swagger
@RestController // 复合注解 创建对象+json格式
@RequestMapping("/shop") //  匹配请求url - 等于是 http://localhost:8080/shop
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口 ： 添加或修改  put
     * http://localhost:8080/shop + {json对象} + put
     */
    @ApiOperation(value = "添加或修改" ) // 打在方法上，对方法做描述
    @PutMapping
    @PreAuthorize(name = "店铺保存", sn = "shop:save")
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {
                shopService.save(shop);
            } else {
                shopService.update(shop);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("操作失败！").setSuccess(false);
        }
    }

    /**
     * 接口 ：删除店铺  delete
     * http://localhost:8080/shop/{id} + 无 + delete
     */
    @ApiOperation(value = "删除店铺" ) // 打在方法上，对方法做描述
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除店铺", sn = "shop:delete")
    public AjaxResult deleteById(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口 ：查询所有店铺  Get
     * http://localhost:8080/shop + 无 + Get
     */
    @ApiOperation(value = "查询所有店铺" ) // 打在方法上，对方法做描述
    @GetMapping
    @PreAuthorize(name = "查询所有店铺", sn = "shop:getAll")
    public List<Shop> queryAll() {
        return shopService.queryAll();
    }

    /**
     * 接口 ：查询单个店铺  Get
     * http://localhost:8080/shop/{id} + 无 + Get
     */
    @ApiOperation(value = "查询单个店铺" ) // 打在方法上，对方法做描述
    @GetMapping("/{id}")
    @PreAuthorize(name = "查询单个店铺", sn = "shop:getOne")
    public Shop queryById(@PathVariable("id") Long id) {
        return shopService.queryById(id);
    }

    /**
     * 接口 ：分页查询  Get
     * http://localhost:8080/shop + {json对象} + Post
     */
    @ApiOperation(value = "分页查询" ) // 打在方法上，对方法做描述
    @PostMapping
    @PreAuthorize(name = "店铺列表", sn = "shop:list")
    public PageList<Shop> queryList(@RequestBody ShopQuery query) {
        return shopService.queryPage(query);
    }

    /**
     * 接口 ：批量删除  patch
     * http://localhost:8080/shop + {json对象} + delete
     */
    @ApiOperation(value = "批量删除" ) // 打在方法上，对方法做描述
    @PatchMapping
    @PreAuthorize(name = "批量删除店铺", sn = "shop:patchDelete")
    // 前端传递过来的数组 我们既可以使用数组接收 也可以使用集合List<Long> 接收
    public AjaxResult patchDelete(@RequestBody Long[] ids) {
        try {
            shopService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口 ：店铺入驻 psot/settlement
     */
    @ApiOperation(value = "店铺入驻" ) // 打在方法上，对方法做描述
    @PostMapping("/settlement")
    public AjaxResult shopS(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (RuntimeException r) {
            r.printStackTrace();
            // 返回Ajax并且 拼接上业务的错误
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！" + r.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
     * 接口 ：店铺审核通过 post/audit/pass
     */
    @ApiOperation(value = "店铺审核通过" ) // 打在方法上，对方法做描述
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog log) {
        try {
            // log中包含了 node(文本) + shopId(店铺的id)
            shopService.pass(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核通过失败！");
        }
    }

    /**
     * 接口 ：店铺入驻驳回 post/audit/reject
     */
    @ApiOperation(value = "店铺入驻驳回" ) // 打在方法上，对方法做描述
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog log) {
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("审核通过失败！");
        }
    }

    /**
     * 导出excel文件
     * @param response
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {
        List<Shop> shops = shopService.queryAll();

        // 要导出的集合 标题 sheet页名字 导出的数据映射给哪个实体对象-类Class excel的名字 response
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "shop.xlsx", response);
    }

    /**
     * 导入excel文件
     * @param file
     */
    @PostMapping("/import")
    // 文件上传三要素 MultipartFile表单对象 type=file 请求post
    public void importExcel(@RequestPart MultipartFile file) {

        // file - 前端点击上传后传递的文件 0 - 标题 1- 头行数 Shop.class - 上传的Excel要映射给哪一个类
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);

        // 动态sql 批量添加
        System.out.println(shops);
    }

    /**
     * 后台首页柱状图
     * @return
     */
    @GetMapping("/echarts")
    public List<ShopDto> getShopState() {
        return shopService.getShopState();
    }

    /**
     * 激活连接跳转登录页面
     * @param id
     * @return
     */
    @GetMapping("/active/{id}")
    public ModelAndView jump(@PathVariable("id") Long id) {
        shopService.shopActive(id);
        // redirect重定向 不共享-可以访问外部链接
        ModelAndView modelAndView = new ModelAndView("redirect:http://localhost:8081/#/login");
        return modelAndView;
    }

}
